package com.mo.lib.utils

import com.dylanc.longan.application
import com.dylanc.longan.pxToDp
import com.dylanc.longan.pxToSp
import okhttp3.internal.and

/**
 * @ author：mo
 * @ data：2019/2/15：9:43
 * @ 功能：单位转换工具类
 */

fun Number.pxToDp(): Int = toFloat().pxToDp()

fun Number.pxToSp(): Int = toFloat().pxToSp()

fun Number.dp2px()= (this.toFloat() * (application.resources.displayMetrics.density) + 0.5f).toInt()

fun dp2px(dp: Int)= dp.dp2px()
fun sp2px(dp: Number)= (dp.toFloat() * application.resources.displayMetrics.scaledDensity + 0.5f).toInt()


/**
 * 十进制数值 转 十六进制字符串
 *
 * @param value 十进制数值
 * @return 十六进制字符串
 */
fun toHexString(value: Int): String {
    return Integer.toHexString(value)
}

private val hexArray = "0123456789ABCDEF".toCharArray()

/**
 * byte 数组 转 十六进制字符串
 *
 * @param bytes byte 数组
 * @return 十六进制字符串
 */
fun bytesToHexString(bytes: ByteArray): String {
    val hexChars = CharArray(bytes.size * 2)
    for (j in bytes.indices) {
        val v: Int = bytes[j] and 0xFF
        hexChars[j * 2] = hexArray[v ushr 4]
        hexChars[j * 2 + 1] = hexArray[v and 0x0F]
    }
    return String(hexChars)
}
